package com.mofeng.myshop.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mofeng.myshop.domain.dto.CommodityDto;
import com.mofeng.myshop.domain.dto.search.CommoditySearchDto;
import com.mofeng.myshop.domain.po.Commodity;
import com.mofeng.myshop.domain.po.CommodityCategory;
import com.mofeng.myshop.domain.po.Project;
import com.mofeng.myshop.domain.vo.CommodityVo;
import com.mofeng.myshop.domain.vo.base.PageMp;
import com.mofeng.myshop.mapper.CommodityCategoryMapper;
import com.mofeng.myshop.mapper.CommodityMapper;
import com.mofeng.myshop.mapper.ProjectMapper;
import com.mofeng.myshop.service.ICommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mofeng.myshop.service.PermissionService;
import com.mofeng.myshop.until.Constants;
import com.mofeng.myshop.until.MyUtil;
import com.mofeng.myshop.until.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author MoFeng
 * @since 2024-04-14
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CommodityServiceImpl implements ICommodityService {

    private final CommodityMapper commodityMapper;

    private final CommodityCategoryMapper commodityCategoryMapper;

    private final ProjectMapper projectMapper;

    private final PermissionService permissionService;

    /**
     * 增加或者修改商品
     *
     * @param commodityDto 商品信息
     * @return R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveCommodity(CommodityDto commodityDto) {
        String id = commodityDto.getId();
        Commodity commodity = commodityDto.toCommodity();
        if (MyUtil.StringUtil.isEmpty(id)) {
            return createCommodity(commodity);
        } else {
            return updateCommodity(commodity);
        }
    }

    /**
     * 添加商品
     *
     * @param commodity 商品信息
     */
    @Transactional(rollbackFor = Exception.class)
    protected R createCommodity(Commodity commodity) {
        String name = commodity.getName();
        if (MyUtil.StringUtil.isEmpty(name)) {
            return R.FAILED("商品名称不能为空");
        }
        String code = commodity.getCode();
        if (MyUtil.StringUtil.isEmpty(code)) {
            return R.FAILED("商品代码不能为空");
        }
        Long commodityCategoryId = commodity.getCommodityCategoryId();
        if (MyUtil.StringUtil.isEmpty(commodityCategoryId)) {
            return R.FAILED("商品类别不能为空");
        }
        CommodityCategory commodityCategory = commodityCategoryMapper.selectById(commodityCategoryId);
        if (commodityCategory == null) {
            return R.FAILED("找不到此商品类别");
        }
        Long projectId = commodity.getProjectId();
        if (MyUtil.StringUtil.isEmpty(projectId)) {
            return R.FAILED("商城不能为空");
        }
        Project project = projectMapper.selectById(projectId);
        if (project == null) {
            return R.FAILED("找不到此商城信息");
        }

        double basePrice = commodity.getBasePrice();
        if (MyUtil.StringUtil.isEmpty(basePrice)) {
            return R.FAILED("商品基础价格不能为空");
        }
        Double multiplierUp = commodity.getMultiplierUp();
        if (MyUtil.StringUtil.isEmpty(multiplierUp)) {
            commodity.setMultiplierUp(1.0);
        }
        Double levelUp = commodity.getLevelUp();
        if (MyUtil.StringUtil.isEmpty(levelUp)) {
            commodity.setLevelUp(null);
        }
        Integer levelTop = commodity.getLevelTop();
        if (MyUtil.StringUtil.isEmpty(levelTop)) {
            commodity.setLevelTop(1);
        }
        Double discount = commodity.getDiscount();
        if (MyUtil.StringUtil.isEmpty(discount)) {
            commodity.setDiscount(0.0);
        }

        commodity.setDelete(Constants.State.DEFAULT_STATE);
        commodity.setHot(Constants.State.DEFAULT_STATE);
        commodityMapper.insert(commodity);
        return R.SUCCESS("商品添加成功").setData(new CommodityVo(commodity));
    }

    /**
     * 修改商品信息
     *
     * @param commodity 商品信息
     */
    @Transactional(rollbackFor = Exception.class)
    protected R updateCommodity(Commodity commodity) {
        Long commodityCategoryId = commodity.getCommodityCategoryId();
        if (!MyUtil.StringUtil.isEmpty(commodityCategoryId)) {
            CommodityCategory commodityCategory = commodityCategoryMapper.selectById(commodityCategoryId);
            if (commodityCategory == null) {
                return R.FAILED("找不到此商品类别");
            }
        }
        commodityMapper.updateById(commodity);
        return R.SUCCESS("商品修改完成");
    }

    /**
     * 根据ID批量删除商品，虚拟删除
     *
     * @param ids ids
     * @return R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R delCommodity(List<String> ids) {
        commodityMapper.deleteBatchIds(ids);
        return R.SUCCESS("商品删除成功");
    }

    /**
     * 查询单个商品
     *
     * @param id id
     */
    @Override
    public R getCommodity(String id) {
        long idLong = Long.parseLong(id);
        Commodity commodity = commodityMapper.selectById(idLong);
        return R.SUCCESS("查询成功").setData(new CommodityVo(commodity));
    }

    /**
     * 获取商品列表
     */
    @Override
    public R getCommodityByPageAdmin(CommoditySearchDto commoditySearch) {
        Page<Commodity> page = commoditySearch.getPageMp();
        String keyword = commoditySearch.getKeyword();
        Long commodityCategoryId = commoditySearch.getCommodityCategoryId();
        Long projectId = commoditySearch.getProjectId();
        String delete = Constants.State.DEFAULT_STATE;
        if (permissionService.MASTER()) {
            delete = null;
        }
        Page<Commodity> pageResult = commodityMapper.findList(keyword, projectId, commodityCategoryId, delete, page);
        return R.SUCCESS("获取商品列表成功").setData(new PageMp<>(pageResult, CommodityVo::new));
    }

    /**
     * 获取商品列表
     *
     * @param commoditySearch 搜索界面
     */
    @Override
    public R getCommodityByPagePortal(CommoditySearchDto commoditySearch) {
        Page<Commodity> page = commoditySearch.getPageMp();
        String keyword = commoditySearch.getKeyword();
        Long commodityCategoryId = commoditySearch.getCommodityCategoryId();
        Long projectId = commoditySearch.getProjectId();
        Page<Commodity> pageResult = commodityMapper.findList(keyword, projectId, commodityCategoryId, Constants.State.DEFAULT_STATE, page);
//        pageResult.getRecords().forEach(commodity -> {
//            System.out.println(commodity.getName());
//        });
        return R.SUCCESS("获取商品列表成功").setData(new PageMp<>(pageResult, CommodityVo::new));
    }


}
